package com.edward.weather.utils;

import net.i2p.crypto.eddsa.EdDSAEngine;
import net.i2p.crypto.eddsa.EdDSAPrivateKey;
import net.i2p.crypto.eddsa.spec.EdDSANamedCurveTable;
import net.i2p.crypto.eddsa.spec.EdDSAParameterSpec;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.charset.StandardCharsets;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.Base64;

public class WeatherUtils {

    private final static Logger logger = LoggerFactory.getLogger(WeatherUtils.class);


    private final static String kid = "CM58FXHF7D";
    private final static String YOUR_PROJECT_ID = "49KVBE9964";
    private final static String YOUR_PROJECT_SECRET = "MC4CAQAwBQYDK2VwBCIEIBSRknVN5AgCvfG2ezvpgh/F05aKCcq0fPwGxNRYjbqr";


    public static String getWeatherIcon(){
        try {
            String privatekey = "-----BEGIN PRIVATE KEY-----\n" +
                    "MC4CAQAwBQYDK2VwBCIEIBSRknVN5AgCvfG2ezvpgh/F05aKCcq0fPwGxNRYjbqr\n" +
                    "-----END PRIVATE KEY-----";

            String replace = privatekey.trim().replace("-----BEGIN PRIVATE KEY-----", "");
            // Private key
            String s1 = replace.trim().replace("-----BEGIN PRIVATE KEY-----", "").replace("-----END PRIVATE KEY-----", "");
            byte[] privateKeyBytes = Base64.getDecoder().decode("-----BEGIN PRIVATE KEY-----MC4CAQAwBQYDK2VwBCIEIBSRknVN5AgCvfG2ezvpgh/F05aKCcq0fPwGxNRYjbqr-----END PRIVATE KEY-----".trim().replace("-----BEGIN PRIVATE KEY-----", "").replace("-----END PRIVATE KEY-----", ""));
            PKCS8EncodedKeySpec encoded = new PKCS8EncodedKeySpec(privateKeyBytes);
            PrivateKey privateKey = new EdDSAPrivateKey(encoded);

            // Header
            //String headerJson = "{\"alg\": \"EdDSA\", \"kid\": \"YOUR_KEY_ID\"}";
            String headerJson = "{\"alg\": \"EdDSA\", \"kid\": "+kid+"}";

            // Payload
            long iat = ZonedDateTime.now(ZoneOffset.UTC).toEpochSecond() - 30;
            long exp = iat + 900;
//            String payloadJson = "{\"sub\": "+YOUR_PROJECT_ID+", \"iat\": " + iat + ", \"exp\": " + exp + "}";
            String payloadJson = "{\"sub\": \"49KVBE9964\", \"iat\": " + iat + ", \"exp\": " + exp + "}";


            // Base64url header+payload
            String headerEncoded = Base64.getUrlEncoder().encodeToString(headerJson.getBytes(StandardCharsets.UTF_8));
            String payloadEncoded = Base64.getUrlEncoder().encodeToString(payloadJson.getBytes(StandardCharsets.UTF_8));
            String data = headerEncoded + "." + payloadEncoded;

            EdDSAParameterSpec spec = EdDSANamedCurveTable.getByName(EdDSANamedCurveTable.ED_25519);

            // Sign
            final Signature s = new EdDSAEngine(MessageDigest.getInstance(spec.getHashAlgorithm()));
            s.initSign(privateKey);
            s.update(data.getBytes(StandardCharsets.UTF_8));
            byte[] signature = s.sign();

            String signatureString = Base64.getUrlEncoder().encodeToString(signature);

            System.out.println("Signature: \n" + signatureString);

            // Print Token
            return data + "." + signatureString;
        }catch (Exception e){
            logger.error("getWeatherIcon error",e);
            return null;
        }
    }

}
